package com.myplans.server.service;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.myplans.criteria.UserSearchCriteria;
import com.myplans.server.util.ServiceLoader;
import com.myplans.shared.AndroidPlan;
import com.myplans.shared.User;
import com.myplans.vo.PlanValue;
import com.myplans.vo.UserValue;

public class AndroidUserServiceImpl extends RemoteServiceServlet implements UserService {

	@Override
	public User signInUser(String email, String password) {
		UserSearchCriteria searchCriteria = new UserSearchCriteria();
		searchCriteria.setEmail(email);
		searchCriteria.setPassword(password);
		
		List<UserValue> userValues = ServiceLoader.getServiceInstance()
				.getUserService().getUsers(searchCriteria);
		
		if (userValues.size() > 0) {
			UserValue userValue = userValues.get(0);
			User user = new User();
			
			try {
				BeanUtils.copyProperties(user, userValue);
				return user;
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return null;
		} else {
			return null;
		}
	}

	@Override
	public User signUpUser(User user) {
		try {
			User signedInUser = signInUser(user.getEmail(), user.getPassword());
			
			if (signedInUser == null) {
				UserValue userValue = new UserValue();
				BeanUtils.copyProperties(userValue, user);
				if (userValue.getPassword() == null) {
					userValue.setPassword(userValue.getEmail());
				}
				userValue = ServiceLoader.getServiceInstance().getUserService().createUser(userValue);
				BeanUtils.copyProperties(user, userValue);
				return user;
			} else {
				return signedInUser;
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public User updateUser(User user) {
		try {
			UserValue userValue = new UserValue();
			BeanUtils.copyProperties(userValue, user);
			userValue = ServiceLoader.getServiceInstance().getUserService().updateUser(userValue);
			BeanUtils.copyProperties(user, userValue);
			
			return user;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public User createPlanForUser(AndroidPlan plan, User user) {
		try {
			UserValue userValue = new UserValue();
			BeanUtils.copyProperties(userValue, user);
			PlanValue planValue = new PlanValue();
			BeanUtils.copyProperties(planValue, plan);
			userValue = ServiceLoader.getServiceInstance().getUserService().createPlanForUser(planValue, userValue);
			BeanUtils.copyProperties(user, userValue);
			
			return user;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public User deletePlanForUser(AndroidPlan plan, User user) {
		try {
			UserValue userValue = new UserValue();
			BeanUtils.copyProperties(userValue, user);
			PlanValue planValue = new PlanValue();
			BeanUtils.copyProperties(planValue, plan);
			userValue = ServiceLoader.getServiceInstance().getUserService().deletePlanForUser(planValue, userValue);
			BeanUtils.copyProperties(user, userValue);
			
			return user;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public AndroidPlan[] getPlansForUser(User user) {
		AndroidPlan[] plans = new AndroidPlan[0];
		try {
			if (user != null) {
				UserValue userValue = new UserValue();
				BeanUtils.copyProperties(userValue, user);
				List<PlanValue> planValues = ServiceLoader.getServiceInstance().
						getUserService().getPlansForUser(userValue);
				plans = new AndroidPlan[planValues.size()];
				int i = 0;
				for (PlanValue planValue : planValues) {
					AndroidPlan plan = new AndroidPlan();
					BeanUtils.copyProperties(plan, planValue);
					plans[i] = plan;
					i++;
				}
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return plans;
	}
	
	@Override
	public User[] getSubscriptionsForUser(User user) {
		User[] subscriptions = new User[0];
		try {
			if (user != null) {
				UserValue userValue = new UserValue();
				BeanUtils.copyProperties(userValue, user);
				List<UserValue> userValues = ServiceLoader.getServiceInstance().
						getUserService().getSubscriptionsForUser(userValue);
				subscriptions = new User[userValues.size()];
				int i = 0;
				for (UserValue userVal : userValues) {
					user = new User();
					BeanUtils.copyProperties(user, userVal);
					subscriptions[i] = user;
					i++;
				}
			}
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return subscriptions;
	}
	
	@Override
	public User createSubscriptionForUser(User subscription, User user) {
		try {
			UserValue userValue = new UserValue();
			BeanUtils.copyProperties(userValue, user);
			UserValue subscriptionValue = new UserValue();
			BeanUtils.copyProperties(subscriptionValue, subscription);
			userValue = ServiceLoader.getServiceInstance().getUserService().createSubscriptionForUser(subscriptionValue, userValue);
			BeanUtils.copyProperties(user, userValue);
			
			return user;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public User deleteSubscriptionForUser(User subscription, User user) {
		try {
			UserValue userValue = new UserValue();
			BeanUtils.copyProperties(userValue, user);
			UserValue subscriptionValue = new UserValue();
			BeanUtils.copyProperties(subscriptionValue, subscription);
			userValue = ServiceLoader.getServiceInstance().getUserService().deleteSubscriptionForUser(subscriptionValue, userValue);
			BeanUtils.copyProperties(user, userValue);
			
			return user;
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
